﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace UMLDesigner.Widgets
{
    public abstract class SlotList
    {
        public SlotList()
        {
            slots = new List<Slot>();
        }

        public SlotList(Point start, int length)
            : this()
        {
            Start = start;
            Length = length;
        }

        public void Add(Slot slot)
        {
            slots.Add(slot);
            RelocateSlots();
        }

        public void Delete(Slot slot)
        {
            slots.Remove(slot);
            RelocateSlots();
        }

        public abstract void RelocateSlots();

        public List<Slot> Slots
        {
            get
            {
                return slots;
            }
        }

        public Point Start
        {
            get;
            set;
        }

        public int Length
        {
            get;
            set;
        }

        private List<Slot> slots;
    }

    public class VerticalSlotList : SlotList
    {
        public VerticalSlotList(Point start, int length)
            : base(start, length)
        {

        }

        public override void RelocateSlots()
        {
            int spacePerSlot = 30;
            int startLoc = (Start.Y) - (((Slots.Count - 1) * spacePerSlot) / 2);

            Slots.Sort(CompareSlots);

            for(int i = 0; i < Slots.Count; i++)
            {
                Slots[i].Location = new Point(Start.X, startLoc + i * spacePerSlot);
            }
        }

        private static int CompareSlots(Slot a, Slot b)
        {
            int y1 = a.Link.DestinationWidget.Location.Y;
            int y2 = b.Link.DestinationWidget.Location.Y;

            if (y1 == y2)
            {
                return 0;
            }
            else if (y1 > y2)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
    }

    public class HorizontalSlotList : SlotList
    {
        public HorizontalSlotList(Point start, int length)
            : base(start, length)
        {

        }

        public override void RelocateSlots()
        {
            int spacePerSlot = 30;
            int startLoc = (Start.X) - (((Slots.Count - 1) * spacePerSlot) / 2);

            Slots.Sort(CompareSlots);

            for (int i = 0; i < Slots.Count; i++)
            {
                Slots[i].Location = new Point(startLoc + i * spacePerSlot, Start.Y);
            }
        }

        private static int CompareSlots(Slot a, Slot b)
        {
            int x1 = a.Link.DestinationWidget.Location.X;
            int x2 = b.Link.DestinationWidget.Location.X;

            if (x1 == x2)
            {
                return 0;
            }
            else if (x1 > x2)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
    }
}
